package cn.zldemo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import android.app.ActivityManager;
import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.*;
import android.transition.Fade;
import android.view.*;
import android.widget.Button;
import android.widget.ImageView;
import cn.zldemo.common.CardViewActivity;
import cn.zldemo.util.SetOnClickUtil;
import cn.zldemo.util.Utils;
import cn.zldemo.util.ViewMapping;
import cn.zldemo.util.ViewMappingUtil;

@ViewMapping(id = R.layout.activity_home)
public class HomeActivity extends AppCompatActivity implements View.OnClickListener {
	@ViewMapping(id = R.id.recycler)
	private RecyclerView mRecyclerView;

	private static Random random = new Random(47);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
		super.onCreate(savedInstanceState);
		ViewMappingUtil.mapView(this, this);
		SetOnClickUtil.registerOnClick(this);
		initView();
	}

	private void initView() {
		initToolBar();
		initRecycler();
	}

	private void initToolBar() {
		Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
		setSupportActionBar(toolbar);
		getSupportActionBar().setDisplayShowTitleEnabled(false);
	}

	private void initRecycler() {
		// use this setting to improve performance if you know that changes

		RecyclerView.LayoutManager layoutManager = new StaggeredGridLayoutManager(3, OrientationHelper.VERTICAL);
		RecyclerView.LayoutManager layoutManager1 = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
		RecyclerView.LayoutManager layoutManager2 = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
		RecyclerView.LayoutManager layoutManager3 = new GridLayoutManager(this, 3);
		mRecyclerView.setLayoutManager(layoutManager);
		mRecyclerView.setAdapter(new Adapter(Arrays.asList("button & background", "status bar & nav bar",
				"cardView & ripple", "toolbar", "switchCompat", "exit", "add1", "add2","viewAnimator demo")));
		// in content do not change the layout size of the RecyclerView
		mRecyclerView.setHasFixedSize(false);
	}

	@Override
	public void onClick(View v) {

	}

	interface OnItemClickListener {
		void onItemClick(View v, int layoutPosition);
	}

	private class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
		private List<String> list;
		private OnItemClickListener listener;

		public Adapter(List<String> list) {
			super();
			this.list = new ArrayList<>(list);
			listener = new OnItemClickListener() {
				@Override
				public void onItemClick(View v, int layoutPosition) {
					onClick(v);
				}
			};
		}

		@Override
		public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
			Context context = parent.getContext();
			if (viewType == 1) {
				ImageView imageView = new ImageView(context);
				imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 150));
				return new MyViewHolder(new ImageView(context));
			}
			Button btn = new Button(context);
			btn.setMinHeight((int) (context.getResources().getDisplayMetrics().density * (36 + random.nextInt(36) + 0.5f)));
			return new MyViewHolder(btn);
		}

		@Override
		public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
			if (position != getItemCount() - 1) {
				((Button) holder.itemView).setText(list.get(position));
			} else {
				((ImageView) holder.itemView).setImageResource(R.mipmap.ic_launcher);
			}
		}

		@Override
		public int getItemViewType(int position) {
			if (position == getItemCount() - 1) {
				return 1;
			}
			return super.getItemViewType(position);
		}

		@Override
		public int getItemCount() {
			return list == null ? 0 : list.size() + 1;
		}

		public void add(int position, String str) {
			list.add(position, str);
			notifyItemInserted(position);
		}

		public void delete(int position) {
			list.remove(position);
			notifyItemRemoved(position);
		}

		@Override
		public void onClick(View v) {
			int index;
			switch ((String) v.getTag()) {
			case "button & background":
				if (Utils.hasLollipop()) {
					getWindow().setExitTransition(new Fade());
				}

				startActivity(
						new Intent(HomeActivity.this, ButtonActivity.class),
						ActivityOptions.makeCustomAnimation(HomeActivity.this, android.R.anim.fade_in,
								android.R.anim.slide_out_right).toBundle());
				break;
			case "status bar & nav bar":
				startActivity(new Intent(HomeActivity.this, StatusBarActivity.class));
				break;
			case "cardView & ripple":
				startActivity(new Intent(HomeActivity.this, CardViewActivity.class));
				break;
			case "toolbar":
				startActivity(new Intent(HomeActivity.this, ToolBarActivity.class));
				break;
			case "switchCompat":
				startActivity(new Intent(HomeActivity.this, SwitchActivity.class));
				break;
			case "exit":
				exit();
				break;
			case "delete 1":
				delete(list.indexOf("delete 1"));
				break;
			case "delete 2":
				delete(list.indexOf("delete 2"));
				break;
			case "add1":
				index = list.indexOf("delete 1");
				if (index < 0) {
					add(list.indexOf("add1"), "delete 1");
				}
				break;
			case "add2":
				index = list.indexOf("delete 2");
				if (index < 0) {
					add(list.indexOf("add1"), "delete 2");
				}
				break;
			}
		}

		private class MyViewHolder extends RecyclerView.ViewHolder {
			public MyViewHolder(View itemView) {
				super(itemView);

				if (listener != null) {
					itemView.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							if (v.getTag() == null) {
								v.setTag(list.get(getLayoutPosition()));
							}
							listener.onItemClick(v, getLayoutPosition());
						}
					});
				}
			}

			@Override
			public String toString() {
				return super.toString();
			}
		}
	}

	private void exit() {
		ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(ACTIVITY_SERVICE);
		if (Utils.hasLollipop()) {
			List<ActivityManager.AppTask> list = activityManager.getAppTasks();
			for (ActivityManager.AppTask appTask : list) {
				appTask.finishAndRemoveTask();
			}
		} else {
			this.finish();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		return super.onPrepareOptionsMenu(menu);
	}
}
